function [time,mean,std] = lpcvd_Merge_SinkToFile(FilenamePattern,FileID,RepeatPerFile)
% [time,mean,std] = lpcvd_Merge(FilenamePattern,beginID,endID,RepeatPerFile)
% DESCRIPTION:
%  Merge the multiple outputs obtained on the cluster into one mean and
%  std.
% 
% EXAMPLE:
%  [time,r2_mean,r2_std]=lpcvd_Merge('Output_r2_model_%d.dat',[1:10],10)
%  merges the output files Output_r2_model_1.dat~Output_r2_model_10.dat.

for i = 1:length(FileID)
    filename = sprintf(FilenamePattern,FileID(i));
    [time,mean_OneFile,std_OneFile] = lpcvd_Load_SinkToFile(filename);
    if i==1
        mean = mean_OneFile;
        var  = RepeatPerFile*std_OneFile.^2;
    else
        mean = mean+mean_OneFile;
        var  = RepeatPerFile*std_OneFile.^2+var;
    end
end
mean = mean/length(FileID);
std = sqrt(var./(length(FileID)*RepeatPerFile));
